package com.umeox.watch.moto.contact.ui;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.ColorDrawable;
import android.media.AudioManager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.umeox.moto.common.log.Logger;
import com.umeox.moto.watch.themes.util.ThemeUtils;
import com.umeox.moto.watch.themes.widget.Slider;
import com.umeox.watch.moto.agora.AgoraService;
import com.umeox.watch.moto.agora.Utils;
import com.umeox.watch.moto.contact.R;


public class VolumeSettingDialog extends Dialog implements DialogInterface.OnShowListener, Slider.OnPositionChangeListener {

    private Slider slVolume;
    private Context mContext;
    private boolean isAnswer;
    private AudioManager mAudioManager;
    private OnVolumeChangeListener mOnVolumeChangeListener;

    public void setOnVolumeChangeListener(OnVolumeChangeListener listener){
        mOnVolumeChangeListener =  listener;
    }

    public VolumeSettingDialog(Context context, boolean isAnswer) {
        super(context);
        mContext = context;
        this.isAnswer = isAnswer;
        createDialog();
    }

    @SuppressLint("ClickableViewAccessibility")
    private void createDialog() {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_volume_settings, null);
        setContentView(view);
        slVolume = view.findViewById(R.id.slVolume);

        if (isAnswer) {
            slVolume.setValueRange(0, 400, false);
        } else {
            mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
            int mRingMaxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_RING);
            int mRingCurrentVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_RING);
            slVolume.setValueRange(0, mRingMaxVolume, false);
            slVolume.setValue(mRingCurrentVolume, false);
        }

        slVolume.setOnPositionChangeListener(this);
        slVolume.onchangeState(true);
        setOnShowListener(this);

        Window window = getWindow();
        window.setBackgroundDrawable(new ColorDrawable(ThemeUtils.windowBackground(getContext(), 0x35454D)));
        window.setGravity(Gravity.CENTER);
        window.setWindowAnimations(R.style.Animation_Uwear_Dialog_SlideVertical);
        WindowManager.LayoutParams params = window.getAttributes();
        params.height = WindowManager.LayoutParams.MATCH_PARENT;
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(params);
    }

    @Override
    public void onShow(DialogInterface dialog) {
        if (isAnswer) {
            int streamVolume = Utils.getVolume(getContext());
            Logger.i("isAnswer=" + isAnswer + ",当前音量(" + streamVolume + ")");
            slVolume.setValueRange(0, 400, false);
            slVolume.setValue(streamVolume, false);
        } else {
            int mRingMaxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_RING);
            int mRingCurrentVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_RING);
            Logger.i("isAnswer=" + isAnswer + ",当前音量(" + mRingCurrentVolume + ")");
            slVolume.setValueRange(0, mRingMaxVolume, false);
            slVolume.setValue(mRingCurrentVolume, false);
        }

    }

    @Override
    public void onPositionChanged(Slider view, boolean fromUser, float oldPos, float newPos, int oldValue, int newValue) {
        Logger.i("isAnswer=" + isAnswer + ",设置音量(" + newValue + ")");
        if (isAnswer) {
            if(mOnVolumeChangeListener != null){
                mOnVolumeChangeListener.onVolumeChanged(newValue);
            }
            Utils.setVolume(getContext(), newValue);
            AgoraService.Companion.getInstance(mContext).adjustPlaybackSignalVolume(newValue);
        } else {
            if (mAudioManager != null) {
                mAudioManager.setStreamVolume(AudioManager.STREAM_RING, newValue, 0);
            }
        }
    }

    public void setIsAnswer(boolean isAnswer) {
        this.isAnswer = isAnswer;
    }

    interface  OnVolumeChangeListener{

        void onVolumeChanged(int volumeValue);

    }
}
